import { ElMessage } from 'element-plus'
import { reactive, computed } from 'vue'

export const useUsers = () => {
    const users = reactive([]) //api返回用户实体
    //api请求查询条件
    const searchForm = reactive({
        username: '',
        realname: ''
    })
    //新增和修改用户信息实体
    const userForm = reactive({
        id: null,
        username: '',
        realname: '',
        email: '',
        phone: '',
        status: 1
    })

    // 过滤用户数据
    const filteredUsers = computed(() => {
        let result = [...mockData.value]

        if (searchForm.username) {
            result = result.filter(user =>
                user.username.includes(searchForm.username)
            )
        }

        if (searchForm.realname) {
            result = result.filter(user =>
                user.realname.includes(searchForm.realname)
            )
        }

        return result
    })

    // 添加用户
    const addUser = (userData) => {
        const newId = Math.max(...mockData.value.map(u => u.id)) + 1
        const newUser = {
            ...userData,
            id: newId,
            createTime: new Date().toLocaleString()
        }
        mockData.value.unshift(newUser)
        ElMessage.success('用户添加成功')
    }

    // 更新用户
    const updateUser = (userData) => {
        const index = mockData.value.findIndex(u => u.id === userData.id)
        if (index !== -1) {
            mockData.value[index] = { ...userData }
            ElMessage.success('用户更新成功')
        }
    }

    // 删除用户
    const deleteUser = async (id) => {
        try {
            await ElMessageBox.confirm('确定要删除该用户吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })

            const index = mockData.value.findIndex(u => u.id === id)
            if (index !== -1) {
                mockData.value.splice(index, 1)
                ElMessage.success('删除成功')
                return true
            }
        } catch {
            // 用户取消删除
            return false
        }
    }

    // 切换用户状态
    const toggleUserStatus = (user) => {
        const index = mockData.value.findIndex(u => u.id === user.id)
        if (index !== -1) {
            const newStatus = user.status === 1 ? 0 : 1
            mockData.value[index].status = newStatus
            const statusText = newStatus === 1 ? '启用' : '禁用'
            ElMessage.success(`已${statusText}用户`)
        }
    }

    // 重置表单
    const resetuserForm = () => {
        Object.assign(userForm, {
            id: null,
            username: '',
            realname: '',
            email: '',
            phone: '',
            status: 1
        })
    }

    // 填充表单数据
    const filluserForm = (user) => {
        Object.assign(userForm, { ...user })
    }

    return {
        users,
        searchForm,
        userForm,
        filteredUsers,        
        addUser,
        updateUser,
        deleteUser,
        toggleUserStatus,
        resetuserForm,
        filluserForm
    }
}